package com.cgnpc.scp.purchase.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.exception.ScpException;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.AppConstantService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.purchase.mybatis.entity.PurchaseCapital;
import com.cgnpc.scp.purchase.service.IPurchaseCapitalService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

/******************************
 * 用途说明: 调试采购管理-资金计划
 * 作者姓名: yblrs
 * 创建时间: 2020/8/20 21:26
 ******************************/
@Controller
@RequestMapping("/scp/purchase/purchaseCapital")
public class PurchaseCapitalController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PurchaseCapitalController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private AppConstantService appConstantService;

    @Autowired
    private IPurchaseCapitalService purchaseCapitalService;

    /**********************************
     * 用途说明: 调试采购管理-资金计划---保存或修改
     * 参数说明: HttpServletRequest request, @RequestBody List<T> reqEntityList
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---保存或修改")
    @UBA(module = "调试采购管理-资金计划", action = "保存或修改", channel = OperatorType.Button)
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object saveOrUpdate(HttpServletRequest request, @RequestBody List<T> reqEntityList) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            ajaxResult = purchaseCapitalService.saveOrUpdate(loginUserInfo, reqLocale, reqEntityList);
        } catch (ScpException e) {
            LOGGER.error("saveOrUpdate.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("saveOrUpdate.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 调试采购管理-资金计划---分页查询
     * 参数说明: T reqEntity
     * 返回值说明: MyPageResult
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---分页查询")
    @UBA(module = "调试采购管理-资金计划", action = "分页查询", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object loadByPaging(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            MyPageResult<T> myPageResult = purchaseCapitalService.loadByPaging(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(myPageResult);
        } catch (Exception e) {
            LOGGER.error("loadByPaging.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 调试采购管理-资金计划---导出Excel
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---导出Excel")
    @UBA(module = "调试采购管理-资金计划", action = "导出Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/exportExcel", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object exportExcel(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            CreateFileInfo createFileInfo = purchaseCapitalService.exportExcel(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(createFileInfo);
        } catch (Exception e) {
            LOGGER.error("exportExcel.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 调试采购管理-资金计划---导入Excel
     * 参数说明: HttpServletRequest request
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---导入Excel")
    @UBA(module = "调试采购管理-资金计划", action = "导入Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/importFromExcel", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object importFromExcel(MultipartHttpServletRequest request) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            MultipartFile reqFile = request.getFile("file");

            ajaxResult = purchaseCapitalService.importFromExcel(loginUserInfo, reqLocale, reqFile);
        } catch (ScpException e) {
            LOGGER.error("importFromExcel.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("importFromExcel.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 调试采购管理-资金计划---删除
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---删除")
    @UBA(module = "调试采购管理-资金计划", action = "删除", channel = OperatorType.Button)
    @RequestMapping(value = "/del", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object del(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            ajaxResult = purchaseCapitalService.del(loginUserInfo, reqLocale, reqEntity);
        } catch (Exception e) {
            LOGGER.error("del.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 调试采购管理-资金计划---根据id查询指定信息
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试采购管理-资金计划---根据id查询指定信息")
    @UBA(module = "调试采购管理-资金计划", action = "根据id查询指定信息", channel = OperatorType.Button)
    @RequestMapping(value = "/loadById", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends PurchaseCapital> Object loadById(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            // UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            T dbEntity = purchaseCapitalService.loadById(reqEntity.getId());

            ajaxResult = AjaxResultUtil.createI18nSuccessDefaultMessage(reqLocale, messageSource, dbEntity);
        } catch (Exception e) {
            LOGGER.error("error;", e);
            ajaxResult = AjaxResultUtil.createI18nErrorDefaultMessage(request, messageSource);
        }

        return ajaxResult;
    }

}
